package com.frikinet.tools.utils;

import java.util.Stack;

public class HistoryHandler {
	
	protected Stack<Object> history;
	protected Stack<Object> forwards;
	protected int size;
	
	public HistoryHandler(int size)
	{
		history = new Stack<Object>();
		forwards = new Stack<Object>();
		this.size = size;
	}
	
	public void add(Object o)
	{
		forwards.clear();
		if(history.size()<size)
		{
			history.push(o);
		}
		else
		{
			history.removeElementAt(0);
			history.push(o);
		}
	}
	
	public Object back(int amount)
	{
		Object o = null;
		if(history.size()>amount)
		{
			for(int i=0;i<amount;i++)
			{
				o = history.pop();
				forwards.push(o);
			}
			o = history.peek();
		}
		return o;
	}
	
	public Object back()
	{
		return this.back(1);
	}
	
	public Object forward(int amount)
	{
		Object o = null;
		if(forwards.size()>=amount)
		{
			for(int i=0;i<amount;i++)
			{
				o = forwards.pop();
				history.push(o);
			}
		}
		return o;
	}
	
	public Object forward()
	{
		return this.forward(1);
	}
	
	public boolean hasBack(int amount)
	{
		return history.size()>amount;
	}
	public boolean hasBack()
	{
		return hasBack(1);
	}
	
	public boolean hasForward(int amount)
	{
		return forwards.size()>=amount;
	}
	public boolean hasForward()
	{
		return hasForward(1);
	}
}
